package com.ihr360.insurance.service.impl;

import com.ihr360.commons.exception.Ihr360Exception;
import com.ihr360.insurance.dao.BenefitCompanySettingReposity;
import com.ihr360.insurance.entity.vo.BenefitCompanySettingEntity;
import com.ihr360.insurance.model.dto.benefits.BenefitCompanySetting;
import com.ihr360.insurance.service.BenefitCompanySettingService;
import com.ihr360.rest.reposity.SimpleRestReposity;
import com.ihr360.rest.service.AbstractSimpleRestServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author david.wei
 */
@Service
public class BenefitCompanySettingServiceImpl extends AbstractSimpleRestServiceImpl<BenefitCompanySetting, Long> implements BenefitCompanySettingService {
    @Autowired
    private BenefitCompanySettingReposity reposity;

    @Override
    public SimpleRestReposity<BenefitCompanySetting, Long> getSimpleRestReposity() {
        return reposity;
    }

    @Override
    public BenefitCompanySetting queryBenefitCompanySetting(String companyId) {
        return reposity.findByCompanyId(companyId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBenefitCompanySetting(String companyId, BenefitCompanySettingEntity entity) {
        if (entity == null) {
            throw new Ihr360Exception(null, "参数不可为空");
        }
        BenefitCompanySetting setting = queryBenefitCompanySetting(companyId);
        if (setting == null) {
            setting = new BenefitCompanySetting();
        }
        setting.setCompanyId(companyId);
        setting.setBenefitApproval(entity.getBenefitApproval());
        reposity.save(setting);
    }
}
